package com.project.class9_1.one.dao.impl;

import com.project.class9_1.one.bean.StudentBean;
import com.project.class9_1.one.dao.IStudentDao;

import java.sql.SQLException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl extends BaseDao implements IStudentDao {
    @Override
    public void add(StudentBean student) {
        this.setConnection();
        try {
            ps = con.prepareStatement("insert into t_student(s_name,s_birthday,s_phone) values(?,?,?)");
            ps.setObject(1,student.getName());
            ps.setObject(2,student.getBirthday());
            ps.setObject(3,student.getPhone());

            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }
    }

    @Override
    public List<StudentBean> findAll() {
        List<StudentBean> list = new ArrayList<>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_student");
            rs = ps.executeQuery();
            while(rs.next()){
                StudentBean s = new StudentBean(rs.getString("s_name"),
                        LocalDate.parse(rs.getString("s_birthday")),rs.getString("s_phone"));
                s.setId(rs.getInt("pk_studentId"));
                list.add(s);
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            this.closeConnection();
        }


        return list;
    }
}
